
class WebSocketServiceClass {

    constructor(webSocketService){

        console.log("WebSocketServiceClass constructor ")
        this.ws = new WebSocket(webSocketService);

        /**
         * 当前ws 状态
         *  0表示还没有链接上
         *  -1表示链接错误，需要重新链接
         *  1表示正常链接，初始化已经完成
         *  2表示客户端主动关闭
         *  3表示服务端主动关闭
         * @type {number}
         */
        this.status = 0;

        this.callBacks = {};

        this.init()
    }

    init(){

        this.bindOpen();

        this.bindMessage();

        this.bindClose();

        this.bindError();

        let the = this;
        window.onbeforeunload = function () {
            the.close();
        }
    }

    bindOpen(){
        let the = this;
        this.ws.onopen = function (event) {
            console.log('ws is open');

            the.addMessageNotSafe("init",{
                "env":"desktop"
            });


        }
    }

    bindMessage(){
        let the = this;
        this.ws.onmessage = function (event) {
            let data = JSON.parse(event.data)

            if(data.code == 200){
                console.log(data.callback)
                switch (data.callback){
                    case 'init':
                        the.status = 1
                        break;
                    case "login":
                        the.call("login",data)
                        break;
                }
            }

        }
    }

    bindClose(){
        let the = this
        this.ws.onclose = function () {
            //不是客户端关闭的就是服务器端关闭的
            console.log("1ws close "+the.status)

            if( the.status != 2 && the.status != -1){
                 the.status = 3;
            }

            the.call("close")

            console.log("2ws close "+the.status)
        }
    }



    bindError(){
        let the = this
        this.ws.onerror = function (data) {

            console.log("ws onerror "+the.status)

            the.status = -1;

            the.call("error")
        }
    }

    close(){
        this.status = 2
        this.ws.close()
    }

    check(scene_id){
        this.addMessage("login",{
            "scene_id":scene_id
        });
    }

    wsSend(data){
        console.log("send data :"+data);
        this.ws.send(data)
    }

    addCallBack(name,callback){
        this.callBacks[name] = callback
    }

    call(name,data){

        let callback =  this.callBacks[name];
        if(callback){
            callback(data,this,name)
        }
    }

    addMessageNotSafe($type,$data) {
        {
            let a =  {
                'type':$type,
                'data':$data
            };

            this.wsSend(JSON.stringify(a));
        }
    }

    addMessage($type,$data) {
        let the = this;
        console.log("YS" + this.status);
        if(this.status == 0){
            setTimeout(function () {
                the.addMessage($type,$data)
            },100);
        }else if(this.status == 1){
            the = this;
            {
                let a =  {
                    'type':$type,
                    'data':$data
                };

                the.wsSend(JSON.stringify(a));
            }
        }
    }
}

window.WebSocketServiceClass = WebSocketServiceClass;